﻿using Anke.SHManage.BLL;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.BM.Controllers
{
    [SkipLoginAttribute]
    public class LoginController : Controller
    {
        //
        // GET: /BM/Login/

        public ActionResult Index()
        {
            string name = Request.Form["username"].ToString();
            string pw = Request.Form["passWord"].ToString();
            string imei = Request.Form["imei"].ToString();

            //首先检查用户名密码
            LoginUserBLL bll = new LoginUserBLL();
            if (bll.Login(name, pw))
            {
                string username = UserOperateContext.Current.Session_UsrInfo.Name;
                string workCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                string role = UserOperateContext.Current.getMaxPerForRole();//获取登录人角色(医生、护士、司机)
                //登录成功，跳转到主页面
                return Json(new { msg = "OK", UName = username, WorkCode = workCode, Role = role }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            else
            {
                //登录失败,发送错误错误给用户
                return Json(new { msg = "Error" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult text()
        {
            return View();
        }

        /// <summary>
        /// 补打发票登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Denglu()
        {
            string name = Request.Form["username"].ToString();
            string pw = Request.Form["passWord"].ToString();
            string imei = Request.Form["imei"].ToString();

            //首先检查用户名密码
            LoginUserBLL bll = new LoginUserBLL();
            if (bll.Login(name, pw))
            {
                string username = UserOperateContext.Current.Session_UsrInfo.Name;
                string workCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                int ID = UserOperateContext.Current.Session_UsrInfo.ID;//用户编码
                string centerCode = "1000";
                //PAD补打发票：只能查看本分中心的人员角色编码
                if (LatePlayRole("LatePlayAllRole"))
                {
                    centerCode = "";
                    return Json(new { msg = "OK", UName = username, WorkCode = workCode, CenterCode = centerCode }, "appliction/json", JsonRequestBehavior.AllowGet);
                }
                else if (LatePlayRole("LatePlayRole"))
                {
                    MobileTaskBLL mtBLL = new MobileTaskBLL();
                    mtBLL.GetCenterCode(workCode, out centerCode);//根据工号获取中心编码

                    //PAD补打发票：查看全部信息的人员角色编码
                    //if (LatePlayRole("LatePlayAllRole"))
                    //{
                    //    centerCode = "";
                    //}
                    //登录成功，跳转到主页面
                    return Json(new { msg = "OK", UName = username, WorkCode = workCode, CenterCode = centerCode }, "appliction/json", JsonRequestBehavior.AllowGet);
                }
                else
                    return Json(new { msg = "没有权限" }, "appliction/json", JsonRequestBehavior.AllowGet);

            }
            else
            {
                //登录失败,发送错误错误给用户
                return Json(new { msg = "登录失败" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
        }

        #region 判断登录人是否有补打发票权限
        public bool LatePlayRole(string Roles)
        {
            string strSuperRole = AppConfig.GetConfigString(Roles);
            string[] tcr = strSuperRole.Split(',');
            bool tarray = false;
            for (int i = 0; i < tcr.Length; i++)
            {
                bool UserRole = UserOperateContext.Current.Session_UsrRole.SingleOrDefault(p => p == Convert.ToInt32(tcr[i])) != 0 ? true : false;
                if (UserRole)
                {
                    //判断登录人角色
                    tarray = true;
                    break;
                }
            }
            return tarray;
        }
        #endregion
    }
}
